<?php
// +----------------------------------------------------------------------+
// | PHP version 4                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.php.net/license/3_0.txt.                                  |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Dmitry Pljonkin <dmitry2029 at mail dot ru>                 |
// +----------------------------------------------------------------------+
// $Id: DM_PerfomanceMeter.php,v 0.9 2005/10/26 19:21:26 pljonkin Exp $

define("DMTIMER_STATE_NOTSTARTED", 0);
define("DMTIMER_STATE_COUNTING", 1);
define("DMTIMER_STATE_PAUSED", 2);
define("DMTIMER_STATE_STOPPED", 3);


class Dm_PerfomanceMeter extends Dm_Timer
{
    var $checkpoints;
    var $silent_mode = true;
    
    function Dm_PerfomanceMeter()
    {
        $this->Dm_Timer();
    }
    
    function checkPoint($name)
    {
        $this->pause();
        $this->checkpoints[$name] = $this->result();
        $this->resume();
    }
    
    // $type = plain | html_comment | html_ul
    function showResults($type='plain')
    {
        if ($type=='plain') {
            foreach ($this->checkpoints as $check=>$time) {
                echo "\n".$check." -> ".$time;
            }
        }
        elseif ($type=='html_comment') {
            echo "<!--";
            foreach ($this->checkpoints as $check=>$time) {
                echo "\n".$check." -> ".$time;
            }
            echo "\n-->";
        }
        elseif ($type=='html_ul') {
            echo "<ul>";
            foreach ($this->checkpoints as $check=>$time) {
                echo "<li>".htmlspecialchars($check)." -&gt; ".$time."</li>";
            }
            echo "</ul>";
        }
    }
    
    function logResults($file)
    {
        if (file_exists($file)) {
            $fp = fopen($file, 'r');
            list($line) = explode("\n",fread($fp, 1024));
            fclose($fp);
            
            $header = implode(';', array_keys($this->checkpoints));
            if ($header!=$line) {
                if (!$this->silent_mode) {
                    echo "Different log file format. Can't append results";
                }
                return false;
            }
            
            $fp = fopen($file, 'a');
            $string = '';
        }
        else {
            $fp = fopen($file, 'w');
            $string = implode(';', array_keys($this->checkpoints))."\n";
        }
        
        $string .= str_replace('.', ',', implode(';', $this->checkpoints))."\n";
        fwrite($fp, $string);
        fclose($fp);
        return true;
    }
}



class Dm_Timer
{
    var $start;
    var $counted;
    var $state;
    
    function Dm_Timer()
    {
        $this->start = 0;
        $this->counted = 0;
        $this->state = DMTIMER_STATE_NOTSTARTED;
    }
    
    function start()
    {
        list($usec, $sec) = explode(" ", microtime());
        $this->start = (float)$usec + (float)$sec;
        $this->state = DMTIMER_STATE_COUNTING;
        return true;
    }
    
    function pause()
    {
        if ($this->state == DMTIMER_STATE_COUNTING) {
            list($usec, $sec) = explode(" ", microtime());
            $this->counted += (float)$usec + (float)$sec - $this->start;
            $this->state = DMTIMER_STATE_PAUSED;
        }
        
        return true;
    }
    
    function resume()
    {
        if ($this->state == DMTIMER_STATE_STOPPED || $this->state == DMTIMER_STATE_NOTSTARTED) {
            return false;
        }
        else if ($this->state == DMTIMER_STATE_PAUSED) {
            $this->start();
            return true;
        }
        else {
            return true;
        }
    }
    
    function stop()
    {
        if ($this->state == DMTIMER_STATE_NOTSTARTED) {
            return false;
        }
        else if ($this->state == DMTIMER_STATE_STOPPED) {
            return true;
        }
        else if ($this->state == DMTIMER_STATE_PAUSED) {
            $this->start();
            return true;
        }
        else {
            list($usec, $sec) = explode(" ", microtime());
            $this->counted += (float)$usec + (float)$sec - $this->start;
            $this->state = DMTIMER_STATE_STOPPED;
            return true;
        }
    }
    
    function result()
    {
        return $this->counted;
    }

}


?>